package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.ChatGroup;
import org.wmq.travelagency02.model.Entity.ContactGroup;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.ContactGroupService;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("contact-group")
@RequiredArgsConstructor
public class ContactGroupController {
    private final ContactGroupService contactGroupService;

    @GetMapping("/all/{masterId}")
    public Result<List<ChatGroup>> getAllChatGroup(@PathVariable String masterId){
        List<ChatGroup> list = this.contactGroupService.getListByMasterId(masterId);
        if (list == null || list.size() == 0){
            return Result.error("暂未加入群组");
        }
        return Result.ok(list);
    }

    @PostMapping
    public Result<Void> addContactGroup(@RequestBody ContactGroup contactGroup){
        contactGroup.setCreateTime(LocalDateTime.now());
        contactGroup.setUpdateTime(LocalDateTime.now());
        this.contactGroupService.save(contactGroup);
        return Result.ok();
    }

    @DeleteMapping("/{masterId}/{chatGroupId}")
    public Result<Void> exitChatGroup(@PathVariable String masterId,@PathVariable String chatGroupId){
        this.contactGroupService.lambdaUpdate().
                eq(ContactGroup::getMasterId,masterId)
                .eq(ContactGroup::getChatGroupId,chatGroupId)
                .remove();
        return Result.ok();
    }
}
